//
//  CJCustomNoNetworkEmptyView.m
//  DatangLife
//
//  Created by 陈建 on 2018/4/22.
//  Copyright © 2018年 DatangLife. All rights reserved.
//

#import "CJCustomNoNetworkEmptyView.h"

@interface CJCustomNoNetworkEmptyView ()
@property (nonatomic, weak) UIImageView *topTipImageView;
@property (nonatomic, weak) UIButton *retryBtn;
@end


@implementation CJCustomNoNetworkEmptyView

- (UIImageView *)topTipImageView{
    if (!_topTipImageView) {
        UIImageView *imgV = [[UIImageView alloc] init];
        [self addSubview:imgV];
        _topTipImageView = imgV;
        imgV.image = [UIImage imageNamed:@"no_NetWork"];
        imgV.layer.masksToBounds = YES;
//        imgV.backgroundColor = [UIColor colorWithHexString:kCommonBgColor];
    }
    return _topTipImageView;
}

- (UIButton *)retryBtn{
    if (!_retryBtn) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:btn];
        _retryBtn = btn;
        btn.backgroundColor = kCommonRedColor;
        [btn setTitle:@"马上重试" forState:UIControlStateNormal];
        [btn setTitleColor:kWhiteColor forState:UIControlStateNormal];
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        btn.titleLabel.font = kFont(15.f);
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.adjustsImageWhenDisabled = NO;
        btn.adjustsImageWhenHighlighted = NO;
    }
    return _retryBtn;
}

- (void)btnClick:(UIButton *)btn{
    if (self.customNoNetworkEmptyViewDidClickRetryHandle) {
        self.customNoNetworkEmptyViewDidClickRetryHandle(self);
    }
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat topTipW = 100;
//    CGFloat topTipX = kScreenWidth / 2.0 - topTipW / 2.0;
//    CGFloat topTipY = 150;
//    CGFloat topTipH = 100;
//    self.topTipImageView.frame = CGRectMake(topTipX, topTipY, topTipW, topTipH);
    [self.topTipImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self).mas_offset(-topTipW/2);
    }];
    
//    CGFloat retryX = topTipX + 20;
//    CGFloat retryY = self.topTipImageView.bottom + 15;
    CGFloat retryW = 100;
    CGFloat retryH = 25;
//    self.retryBtn.frame = CGRectMake(retryX, retryY, retryW, retryH);
    [self.retryBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.topTipImageView);
        make.top.equalTo(self.topTipImageView.mas_bottom).offset(15);
        make.width.equalTo(@(retryW));
        make.height.equalTo(@(retryH));
    }];
}

@end
